package com.example.service;

import com.example.dao.*;
import com.example.dao.impl.*;
import com.example.entity.IndexInfo;
import com.example.entity.customerAdmin.*;
import com.example.utils.ResultData;
import com.example.vo.ConFollow;
import com.example.vo.DetailExtra;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
@Data
@Service
public class CustomService {
    CustomerAdminDao customDao=new CustomerAdminDaoImpl();
    CustomDao customDao1=new CustomDaoImpl();
    ContactUserDao contactUserDao=new ContactUserDaoImpl();
    public List<IndexInfo> getBtn(int pid) {
        return customDao.getBtn(pid);
    }

    public ResultData selectAll(int page, int limit) {
        int newPage=(page-1)*limit;
        List<CustomerAdmin> customers = customDao.selectAll(newPage, limit);
        ResultData resultData=new ResultData();
        resultData.setCode(0);
        resultData.setMsg("查询成功");
        resultData.setCount(1000);
        resultData.setData(customers);
        return resultData;
    }

    public ResultData selectByName(String uname) {
     List<CustomerAdmin> customList=customDao.selectByName(uname);
        ResultData resultData=new ResultData();
        resultData.setCode(0);
        resultData.setMsg("查询成功");
      resultData.setData(customList);
        return resultData;
    }

    public ResultData selectPart(CustomerAdmin custom) {
        //转日期对象
        List<CustomerAdmin> cusList=new ArrayList<>();
        List<CustomerAdmin> customerAdmins = customDao.selectAll(0, 1000);
        for (CustomerAdmin custom1:customerAdmins) {
            if(custom1.getType()==custom.getType()&&custom1.getName().equals(custom.getName())&&custom1.getStates().equals(custom.getStates())&&custom1.getStarRating().equals(custom.getStarRating())){
                cusList.add(custom1);
            }
        }
        ResultData resultData=new ResultData();
        resultData.setCode(0);
        resultData.setData(cusList);
        return resultData;
    }

    public ResultData selectById(int id) {
        ResultData resultData=new ResultData();
        List<CustomerAdmin> customerAdmins = customDao.selectById(id);
        resultData.setData(customerAdmins);
        resultData.setCode(200);
        return resultData;
    }

    public ResultData selectCusName(String name) {
        ResultData resultData=new ResultData();
        List<Custom>customList= customDao1.selectCusName(name);
        resultData.setCode(200);
        resultData.setData(customList);
        return resultData;
    }

    public ResultData selectContact(int id) {
        List<ContactUser>contactUserList=new ArrayList<>();
        ResultData resultData=new ResultData();
        ContactUser contactUser = contactUserDao.selectId(id);
        contactUserList.add(contactUser);
        resultData.setCode(200);
        resultData.setData(contactUserList);
        return resultData;
    }

    public ResultData selectExtreById(int id) {
        List<DetailExtra>detailExtraList=new ArrayList<>();
        ExtraDao extraDao=new ExtraDaoImpl();
        ResultData resultData=new ResultData();
        Custom custom = customDao1.selectCusById(id);
        List<Extra> extraList=extraDao.selectById(id);
        List<Custom> customList = customDao1.selectBelCus(id);
        int num=customList.size();
        DetailExtra detailExtra=new DetailExtra();
        for (Extra extra:extraList) {
            detailExtra.setArea(extra.getArea());
            detailExtra.setAddress(extra.getAddress());
            detailExtra.setScale(extra.getScale());
            detailExtra.setParentName(custom.getName());
            detailExtra.setLowNum(num);
            detailExtra.setExtraInfo(extra.getRmkTip());
            detailExtraList.add(detailExtra);
        }
        resultData.setCode(200);
        resultData.setData(extraList);
        return resultData;
    }

    public ResultData selectFollowDetail(String name) {
        List<ConFollow>conFollowList=new ArrayList<>();
        ResultData resultData=new ResultData();
        FollowDao followDao=new FollowDaoImpl();
        String newContact=followDao.selectFolConInfo(name);
        String prevContact=followDao.selectprevConInfo(name);
            ConFollow conFollow=new ConFollow();
            ContactUser contactUser=contactUserDao.selectByName(newContact);
            ContactUser prevContactUser=contactUserDao.selectByName(prevContact);
           String updateTime=followDao.selectUpdateTime(name);
            String createTime=followDao.selectCreateTime(name);
           List<ContactUser>contactUserList= contactUserDao.selectHelpContact(contactUser.getId());
           List<String> helpContact=new ArrayList<>();
            for (ContactUser contactUser1:contactUserList) {
                helpContact.add(contactUser1.getName());
            }
            conFollow.setHelpContact(helpContact);
            conFollow.setDept(contactUser.getRole());
            conFollow.setCreatePeople(contactUser.getName());
            conFollow.setCreateTime(createTime);
            conFollow.setUpdateTime(updateTime);
            conFollow.setFinishTime(updateTime);
            conFollow.setSecTime(updateTime);
            conFollowList.add(conFollow);
        resultData.setData(conFollowList);
        resultData.setCode(200);
        return resultData;
    }

    public ResultData selectBelCus(String name) {
        ResultData resultData=new ResultData();
        for (Custom custom : customDao1.selectCusName(name)) {
            List<Custom>customList=customDao1.selectBelCus(custom.getId());
            resultData.setCode(0);
            resultData.setData(customList);
        }
        return resultData;

    }

    public ResultData isUname(String name) {
        ResultData resultData=new ResultData();
        List<Custom> customList = customDao1.selectCusName(name);
        if(customList.size()==0){
            resultData.setCode(404);
        }else {
            resultData.setCode(200);
        }
        return resultData;
    }

    public ResultData addCusConExCusAd(ContactUser contactUser, Custom custom, Extra extra, Follow follow) {
        ResultData resultData=new ResultData();
        int i = contactUserDao.add(contactUser);
        if(i>0){
            ContactUser contactUser1 = contactUserDao.selectByName(contactUser.getName());
            custom.setContactId(contactUser1.getId());
            int j = customDao1.add(custom);
            if(j>0){
                List<Custom> customList = customDao1.selectCusName(custom.getName());
                for (Custom custom1:customList) {
                    extra.setCusId(custom1.getId());
                }
                ExtraDao extraDao=new ExtraDaoImpl();
                int k = extraDao.add(extra);
                if(k>0){
                    FollowDao followDao=new FollowDaoImpl();
                    int l = followDao.add(follow);
                    if(l>0){
                        //添加完全信息表
                        CustomerAdmin customerAdmin=new CustomerAdmin();
                        customerAdmin.setName(custom.getName());
                        customerAdmin.setStarRating(custom.getCusStar());
                        customerAdmin.setSaleUser(contactUser1.getName());
                        customerAdmin.setPhone(contactUser1.getPhone());
                        customerAdmin.setStates(custom.getCusState());
                        customDao.add(customerAdmin);
                        resultData.setCode(200);
                        return resultData;
                    }
                }
            }
        }
        resultData.setCode(404);
        return resultData;
    }

    public ResultData selectAll() {
        ResultData resultData=new ResultData();
        List<Custom>customList=customDao1.selectAll();
        resultData.setCode(200);
        resultData.setData(customList);
        return resultData;
    }
}
